<!DOCTYPE html>
<html>
  <head>
	<meta charset="gbk">
  </head>
  <body>
	<script type="text/javascript" src="lib/store.js"></script> 
	<script src=" http://tjs.sjs.sinajs.cn/open/api/js/wb.js?appkey=3916367922" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript" src="jquery.min.js"></script> 
	<script>
	var settings = new Store("settings", {});
	var options = settings.toObject();
	

	chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});
	chrome.browserAction.setBadgeText({text:String(50)});

		/* show page action */
		function showPageAction(tabId, changeInfo, tab) {
			if (tab.url.search("weibo.com") > -1 || tab.url.search("www.weibo.com") > -1) {
			  chrome.pageAction.show(tabId);
			}
		};
		
		/* send options*/
		function getOptions(callback) {
			callback(options);
		};
		
		/* process the requests */
		function onRequest(request, sender, callback) {
			if (request.action == 'getOptions') {
				getOptions(callback);
			}
			else if (request.action == 'refresh') {
				chrome.windows.getCurrent(function(win) {
					var cwin = win.id;
					chrome.tabs.getAllInWindow(cwin, function(tabs) {
						for (var i = 0; i < tabs.length; i++) {
							var t = tabs[i].url;
							if (t.match('weibo.com') || t.match('www.weibo.com')) {
								var tab = tabs[i];
								chrome.tabs.update(tab.id, {url: tab.url, selected: tab.selected}, null);
							}
						}
					});
				});
			}
			else if (request.action == 'redirect' ) {
				chrome.tabs.create({url: request.url, selected: tab.selected}, null);
			}
			else if (request.action == 'notify' ) {
				var icon = chrome.extension.getURL("img/icon/icon_48.png");
				var title = "ÄãÓÐÐÂÏûÏ¢";
				var body = "";
				var notify_value = request.value;
				if ( notify_value > 7 ) {
					body += "ÐÂÎ¢²©\n";
					notify_value -= 8;
				}
				if ( notify_value > 3 ) {
					body += "ÐÂÆÀÂÛ";
					notify_value -= 4;
				}
				if ( notify_value > 1 ) {
					body += "ÐÂ@ÎÒ\n";
					notify_value -= 2;
				}
				if ( notify_value > 0 ) {
					body += "ÐÂË½ÐÅ\n";
					notify_value -= 1;
				}
				
				var popup = webkitNotifications.createNotification( icon, title, body );
				popup.replaceId = this.replaceId;
				var settings = new Store("settings", {});
     			var autoclose = settings.get( "notification_time" );
				popup.ondisplay = function(event) {  
                        setTimeout(function() {  
                            event.currentTarget.cancel();  
                        }, autoclose * 1000 );  
                    };
				popup.show();
			}
		};
		chrome.extension.onRequest.addListener(onRequest);
		chrome.tabs.onUpdated.addListener(showPageAction);
    </script>
  </body>
</html>